<template>
	<view class="content">
		<image class="logo-box" :src="logo" mode="aspectFit"></image>
		<view class="text-area c-c-c">
			<text class="basic-title">欢迎登录{{title}}</text>
		</view>
		<!-- withCredentials="true" 是否带上登录信息 -->
		<button class="login-box r-nw-c-c" open-type="getUserInfo"  withCredentials="true" @getuserinfo="getuserInfo">
			<image class="icon-wx" src="/static/image/wx.png"></image>
			<span class="text basic-title">微信授权登录</span>
		</button>
		<view class="privacy-content">
			<label class="privacy-item">
				<checkbox-group @change="checkagree">
					<checkbox value="1" :checked="false" style="transform: scale(.7)"/><text>请阅读并同意</text>
				</checkbox-group>
			</label>
			<navigator class="privacy-item nav-link" url="/pages/index/agreement">《用户协议》</navigator>
			<navigator class="privacy-item nav-link" url="/pages/index/privacy">《隐私政策》</navigator>
		</view>
		
		<!-- <button type="default" @getphonenumber="getPhoneNumber" open-type="getPhoneNumber">手机号登录</button> -->
<!-- 		<view class="login-box r-nw-c-c" @click="login()">
			<image class="icon-wx" src="/static/image/wx.png"></image>
			<span class="text basic-title">微信授权登录</span>
		</view> -->
	</view>
</template>

<script>
	import { login, configs }  from "@/utils/api.js";
	export default {
		data() {
			return {
				title: '易方行租车',
				logo: '',
				userInfo: {
					code: '',
					nickname: '',
					avatar: ''
				},
				path: '',
				id: -1,
				isAgreement: 0
			}
		},
		onLoad() {
			this.path = uni.getStorageSync('path');
			// 获取code
			uni.login({
			    provider: 'weixin',
			    success: res => {
					this.userInfo.code = res.code;
				},
				fail: err => {}
			})
			this.getConfigs();
		},
		methods: {
			// 获取logo
			getConfigs() {
				configs().then(res => {
					this.logo = res.data.logo;
				})
			},
			// 获取code及用户信息
			getuserInfo  ({ detail }) {
				if(this.isAgreement != 1){
					return uni.showToast({
								icon: 'none',
								title: '请阅读并同意用户以及隐私协议'
							})
				}
				this.userInfo.nickname = detail?.userInfo.nickName;
				this.userInfo.avatar = detail?.userInfo.avatarUrl;
				uni.showModal({
					title:'授权',
					content:"微信授权登录",
					success: () => {
						// 获取用户信息成功, info.authResult保存用户信息
						login(this.userInfo).then(res => {
							if(res.code === 200) {
								uni.setStorageSync('access_token', res.data.token);
								uni.showToast({
									icon: 'none',
									title: '登录成功'
								})
								// 返回上一页
								uni.setStorageSync('is-auth-back', true);
								this.gateBack();
							}
						})
					}
				})
			},
			gateBack() {
				if(this.path === '/pages/index/cartType') {
					uni.navigateBack()
				} else {
					uni.switchTab({
						url: this.path
					})
				}
				uni.removeStorageSync('path');
			},
			checkagree(e, a){
				this.isAgreement = e.detail.value[0]
			}
		}
	}
</script>

<style lang="scss">
	.basic-title {
		font-family: PingFangSC, PingFang SC;
		font-weight: 500;
		font-size: 40rpx;
		color: #333333;
	}
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo-box {
		margin: 200rpx auto 50rpx;
		width: 176rpx;
		height: 176rpx;
		border-radius: 50%;
		.title {
			color: #FFFFFF;
		}
	}
	.login-box{
		margin: 118rpx auto 0;
		width: 702rpx;
		height: 88rpx;
		background: linear-gradient( 270deg, #FF7F00 0%, #FF3735 100%);
		border-radius: 16rpx;
		.icon-wx {
			margin-right: 10rpx;
			width: 59rpx;
			height: 48rpx;
		}
		.text {
			font-weight: 600;
			font-size: 32rpx;
			color: #FFFFFF;
		}
	}
	.privacy-content {
		display: flex; justify-content: center; font-size: 26rpx; line-height: 1.2; padding-top: 60rpx;
		.privacy-item { display: flex;align-items: center; }
		.nav-link {
			color: #FF7F00;
		}
	}
</style>
